require('es6-promise').polyfill();

import axios from 'axios'

function loadSysRoleMenuData(data){
  return {
    type: 'LOAD_SYSROLE_MENU_DATA',
    data
  }
}

function loadSysRoleMenuByRoleData(data){
  return {
    type: 'LOAD_SYSROLE_MENU_ROLE',
    data
  }
}

export function apiMsgAction(msg) {
  return {
    type: 'API_MSG',
    msg
  }
}

export function loadSysRoleMenuDataAsync() {
  return function(dispatch) {
    return axios.post('/api/sysRoleMenu/').then(
      resp => dispatch(loadSysRoleMenuData(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function loadSysRoleMenuByRoleAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysRoleMenu/queryByRole', data).then(
      resp => dispatch(loadSysRoleMenuByRoleData(resp.data)),
      error => dispatch(apiMsgAction(error.data.errMsg))
    )
  }
}

export function createSysRoleMenuAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysRoleMenu/addMenu',data).then(
      resp => dispatch(loadSysRoleMenuByRoleAsync({
        role_id: data.role_id
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}

export function deleteSysRoleMenuAsync(data) {
  return function(dispatch) {
    return axios.post('/api/sysRoleMenu/deleteMenu',data).then(
      resp => dispatch(loadSysRoleMenuByRoleAsync({
        role_id: data.role_id
      })),
      error => dispatch(apiMsgAction(error.data.errMsg))
    );
  };
}
